<template>
  <div id="customerManager">
    <!-- <h1>这是顾客管理页面</h1> -->
    <!-- 搜索框 -->
    <div class="customerManager_top">
      <el-col :span="2">
        <el-button type="primary" @click="addCustomer">添加</el-button>
      </el-col>
      <el-col :span="4">
        <el-input v-model="input" placeholder="请输入状态" class="status" />
      </el-col>
      <el-col :span="2">
        <el-button type="primary" @click="search">查询</el-button>
      </el-col>
    </div>
    <!-- 表格 -->
    <div class="customerManager_center">
      <el-table :data="customers" stripe style="width: 100%">
        <el-table-column prop="id" label="编号" width="180" align="center" />
        <el-table-column prop="realname" label="姓名" align="center" />
        <el-table-column prop="telephone" label="手机号" align="center" />
        <el-table-column prop="status" label="状态" align="center" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="delById(scope.$index,scope.row)">删除</el-button>
            <el-button type="text" size="small" @click="changeCustomer(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="details(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页按钮 -->
    <div class="customerManager_footer">
      <el-pagination :current-page.sync="currentPage1" :page-size="pageSize" layout="total, prev, pager, next" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <!-- 添加模态框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form ref="myform" :model="form" :rules="rules" label-width="100px" class="demo-form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model.number="form.telephone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="禁用" value="禁用" />
            <el-option label="启用" value="启用" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('myform')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import qs from 'querystring'// 转化为查询字符串
export default {
  data() {
    return {
      input: '', // 搜索输入框
      customers: [], // 表格

      dialogFormVisible: false,
      title: '添加顾客信息',
      form: {
        username: '',
        realname: '',
        telephone: '',
        status: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]

      },

      currentPage1: 1, // 分页按钮
      pageSize: 6,
      total: 0
    }
  },
  created() {
    this.findCustomers()
  },
  methods: {
    // 给确定按钮绑定事件
    submitForm(formName) {
      var self = this
      // 1.获取表单数据+表单验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          // 2.发送请求
          self.$axios.post('/customer/saveOrUpdate', qs.stringify(self.form)).then(res => {
            if (res.data.status == 200) {//eslint-disable-line
              // 保存成功
              self.$message({
                message: '恭喜你，保存成功',
                type: 'success'
              })
              self.findCustomers()
            } else {
              // 保存失败
              self.$message.error('保存失败')
            }
            self.dialogFormVisible = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 给添加按钮绑定事件
    addCustomer() {
      this.dialogFormVisible = true
      this.title = '添加顾客信息'
      console.log(this.form, '-------')
      this.form = {
        username: '',
        realname: '',
        telephone: '',
        status: '',
        password: ''
      }
    },
    // 给编辑按钮绑定事件
    changeCustomer(row) {
      this.dialogFormVisible = true
      this.title = '修改顾客信息'
      // console.log(row);
      this.form = {
        id: row.id,
        username: row.username,
        realname: row.realname,
        telephone: row.telephone,
        status: row.status,
        password: row.password
      }
    },
    // 给搜索按钮绑定事件
    search() {
      // 1.获取用户输入内容
      // alert(this.input);
      if (this.input) {
        this.findCustomers(this.input)
      } else {
        this.findCustomers()
      }
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage1 = val
      this.findCustomers()
    },
    // 详情按钮的跳转
    details(id) {
      // alert(id);
      this.$router.push({ path: '/customer-details/index', query: { id: id }})
    },
    // 封装函数  分页查询所有数据
    findCustomers(status) {
      if (status) {
        var data = {
          page: this.currentPage1 - 1,
          pageSize: this.pageSize,
          status: status
        }
      } else {
        data = {
          page: this.currentPage1 - 1,
          pageSize: this.pageSize
        }
      }
      console.log(data)
      this.$axios.post('customer/query', qs.stringify(data)).then(res => {
        //   console.log(res)
        this.total = res.data.data.total
        this.customers = res.data.data.list
      })
    },
    // 给删除按钮绑定事件
    delById(index, row) {
      //   console.log(row)
      var id = row.id// index为第几行   row为当前行的数据
      var self = this
      this.$confirm('此操作将永久删除这条数据, 是否继续?', '提示', {

        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.get('/customer/deleteById', {
          params: { id }
        }).then((response) => {
          if (response.data.status == 200) {//eslint-disable-line
            // 删除成功
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            self.findCustomers()
          } else {
            // 删除失败
            self.$message({
              showClose: true,
              message: '删除失败',
              type: 'error'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }

}
</script>
<style lang="scss" scoped>
    #customerManager{
        padding: 20px;
        .status{
            padding-right: 20px;
        }
    }

</style>
